﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Experimental.UIElements;
using UnityEngine;
using UnityEngine.UI;

public class JsonSubEditor : EditorWindow
{
    private Vector2 scroPos;
    public TableClass _table;
    public TableSetClass _tableSet;
    private GUIStyle _style;

    private int needWidth;
    private int needHeight;

    public static JsonSubEditor JsonSub;

//
//    [MenuItem("工具/Json编辑")]
//    static void Init()
//    {
//        JsonSub = GetWindow<JsonSubEditor>();
//        JsonSub.titleContent = new GUIContent("Json子表编辑", Resources.Load<Texture>("Icon/eg"));
//    }

    private void OnGUI()
    {
        _style = new GUIStyle("box");
        _style.normal.textColor = Color.magenta;
        scroPos = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height),
            scroPos, new Rect(0, 0, needWidth, needHeight));

        GUI.Label(new Rect(10, 10, 40, 20), "路径:");
        GUI.Label(new Rect(50, 10, 700, 20), "子表编辑");
        if (GUI.Button(new Rect(10, 35, 40, 20), "新建"))
        {
            NewBtn();
        }

        if (GUI.Button(new Rect(55, 35, 40, 20), "保存"))
        {
            SaveBtn();
        }

        if (GUI.Button(new Rect(100, 35, 40, 20), "打开"))
        {
            OpenBtn();
        }

        GUI.Label(new Rect(10, 60, 700, 20), "===========================================================");
        ShowType();
        ShowTag();
        ShowTable();
        GUI.EndScrollView();
    }

    private void ShowType()
    {
        if (_table != null && _table.typeList != null && _table.keyList.Count > 0)
        {
            for (int i = 0; i < _table.typeList.Count; i++)
            {
//                TypeEnum oldtype = _table.typeList[i];
                _table.typeList[i] =
                    (TypeEnum) EditorGUI.EnumPopup(new Rect(10 + i * 75, 80, 73, 20), _table.typeList[i]);
//                if (oldtype != _table.typeList[i] && _table.typeList[i] == TypeEnum.List)
//                {
//                    for (int j = 0; j < _tableSet.valueList.Count; j++)
//                    {
//                        if (!(_tableSet.valueList[j].valueList[i] is Table))
//                        {
//                            _tableSet.valueList[j].valueList[i] = new Table();
//                        }
//                    }
//                }
            }
        }
    }

    private void ShowTag()
    {
        if (_table != null && _table.keyList != null)
        {
            if (_table.keyList.Count > 0)
            {
                for (int i = 0; i < _table.keyList.Count; i++)
                {
                    _table.keyList[i] = GUI.TextField(new Rect(10 + i * 75, 100, 73, 20),
                        _table.keyList[i], _style);
                    if (GUI.Button(new Rect(10 + i * 75, 125, 73, 20), "删除"))
                    {
                        _table.keyList.RemoveAt(i);
                        _table.typeList.RemoveAt(i);
                        for (int j = 0; j < _tableSet.valueList.Count; j++)
                        {
                            _tableSet.valueList[j].valueList.RemoveAt(i);
                        }
                    }
                }
            }


            if (GUI.Button(new Rect(10 + _table.keyList.Count * 75, 100, 60, 20), "添加"))
            {
                _table.keyList.Add("new tag");
                _table.typeList.Add(TypeEnum.String);
                for (int i = 0; i < _tableSet.valueList.Count; i++)
                {
                    _tableSet.valueList[i].valueList.Add("value" + _tableSet.valueList[i].valueList.Count);
                }
            }

            needWidth = 160 + _table.keyList.Count * 75;
        }
        else
        {
            needWidth = 0;
            needHeight = 0;
        }
    }

    private void ShowTable()
    {
        if (_table != null && _table.keyList != null)
        {
            for (int i = 0; i < _tableSet.valueList.Count; i++)
            {
                for (int j = 0; j < _tableSet.valueList[i].valueList.Count; j++)
                {
//                    if (_table.typeList[j] != TypeEnum.List)
                    {
                        _tableSet.valueList[i].valueList[j] = GUI.TextField(new Rect(10 + j * 75, 155 + i * 25, 73, 20),
                            _tableSet.valueList[i].valueList[j].ToString());
                    }
//                    else
//                    {
//                        if (GUI.Button(new Rect(10 + j * 75, 155 + i * 25, 73, 20),"编辑"))
//                        {
//                            Debug.Log("列表" + i + "," + j);
//                            JsonSubEditor jse = GetWindow<JsonSubEditor>();
//                            Table tb = (Table) _tableSet.valueList[i].valueList[j];
//                            jse._table = tb.talbe;
//                            jse._tableSet = tb.tableSet;
//                            jse.Show();
//                        }
//                    }
                }

                if (GUI.Button(new Rect(10 + _table.keyList.Count * 75, 155 + i * 25, 60, 20), "添加"))
                {
                    TableDataClass tableDataClass = new TableDataClass(_table.typeList);
                    _tableSet.valueList.Insert(i + 1, tableDataClass);
                }

                if (GUI.Button(new Rect(85 + _table.keyList.Count * 75, 155 + i * 25, 60, 20), "删除"))
                {
                    _tableSet.valueList.RemoveAt(i);
                }
            }

            needHeight = 160 + _tableSet.valueList.Count * 25;
        }
        else
        {
            needHeight = 0;
            needWidth = 0;
        }
    }

    private void NewBtn()
    {
        _table = new TableClass();
        _tableSet = new TableSetClass();
    }

    private void SaveBtn()
    {
        Close();
    }

    private void OpenBtn()
    {
        if (Selection.activeObject is TextAsset)
        {
            Debug.Log("ssss");
        }
    }

    private int CheckContent(int lineIndex)
    {
        int resIndex = -1;
        for (int i = 0; i < _table.typeList.Count; i++)
        {
            if (_table.typeList[i] == TypeEnum.Int)
            {
                int v;
                bool res = int.TryParse(_tableSet.valueList[lineIndex].valueList[i].ToString(), out v);
                if (!res)
                {
                    resIndex = i;
                }
            }
            else if (_table.typeList[i] == TypeEnum.Float)
            {
                float v;
                bool res = float.TryParse(_tableSet.valueList[lineIndex].valueList[i].ToString(), out v);
                if (!res)
                {
                    resIndex = i;
                }
            }
//            else if (_table.typeList[i] == TypeEnum.List)
//            {
//                bool res = _tableSet.valueList[lineIndex].valueList[i].ToString().StartsWith("[") &&
//                           _tableSet.valueList[lineIndex].valueList[i].ToString().EndsWith("]");
//                if (!res)
//                {
//                    resIndex = i;
//                }
//            }
        }

        return resIndex;
    }
}